#include <stdio.h>
#include <stdlib.h>

typedef char DataType;


typedef struct node {

    DataType data;
    struct node *last;
    struct node *next;

} LinkNode;

typedef LinkNode LinkList;


LinkList *init() {

    LinkList *head = (LinkList *) malloc(sizeof(LinkNode));
    head->data = '0';

    head->next = head;
    head->last = head;

    DataType ch;
    LinkNode *lastItem = head;

    while ((ch = getchar()) != '\n') {

        LinkNode *item = (LinkNode *) malloc(sizeof(LinkNode));
        item->data = ch;

        item->last = lastItem;
        item->next = lastItem->next;

        lastItem->next = item;

        lastItem = item;
    }

    return head;
}

/**
 * 获取链表的第I个节点
 * @param list
 * @param index
 * @return
 */
LinkNode *getIndex(LinkList *list, int index) {

    LinkNode *p = list->next;
    int i = 1;

    while (p != list && index > i) {
        p = p->next;
        i++;
    }
    return p;
}

DataType deleteItem(LinkNode *item) {

    if (NULL == item) {
        printf("node is null");
        return '0';
    }

    DataType data = item->data;





}


/**
 * 将元素插入到指定节点之前
 * @param node
 * @param data
 */
void insertLast(LinkNode *node, DataType data) {

    if (NULL == node) {
        printf("node is null");
        return;
    }

    LinkNode *new = (LinkNode *) malloc(sizeof(LinkNode));

    new->data = data;
    new->last = node->last;
    new->next = node;

    node->last->next = new;
    node->last = new;
}


void toString(LinkList *list) {

    LinkNode *p = list->next;
    printf("[");

    while (p != NULL && p != list) {
        printf("%c,", p->data);
        p = p->next;
    }
    printf("]\n");

}


int main() {
    printf("Please input some key. example: abcde\n");

    LinkList *list = init();
    printf("top head =%c\n", list->data);

    toString(list);

    LinkNode *item = getIndex(list, 3);
    printf("getIndex,3=%c\n", item->data);

    insertLast(item, 'z');
    toString(list);




    return 0;
}
